package com.taomu.app.zongchouhuli.moudle.login.activity;

import android.app.Dialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.taomu.app.zongchouhuli.framework.BaseActivity;
import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.moudle.login.contract.RegisterContract;
import com.taomu.app.zongchouhuli.moudle.login.contract.impl.RegisterPresenter;

/**
 * 忘记密码
 */
public class ForgetActivity extends BaseActivity<RegisterContract.Presenter> implements RegisterContract.View {

    private TextView mSmsTv;

    private EditText mSmsEt;

    private EditText mAccountEt;

    private EditText mPwdEt;

    private TextView mSubmitTv;

    private ImageView mBackIm;

    @Override
    protected void onDestroy() {
        mPresenter.onDetch();
        super.onDestroy();
    }

    @Override
    protected int getContentLayout() {
        return R.layout.fragment_register;
    }

    @Override
    protected void initView() {
        mAccountEt = findViewById(R.id.register_user_et);
        mPwdEt = findViewById(R.id.register_pwd_et);
        mSmsEt = findViewById(R.id.register_sms_et);
        mSmsTv = findViewById(R.id.register_sms_tv);
        mSubmitTv = findViewById(R.id.register_submit_tv);
        mBackIm = findViewById(R.id.register_back_im);
        mSubmitTv.setText("完成");
    }

    @Override
    protected void initData() {
        //mPresenter.isSumbmitCanClick(mAccountEt,mSmsEt,mPwdEt);
        //mPresenter.isSmsCanClick(mAccountEt);
    }

    @Override
    protected void initEvent() {
        mSmsTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!TextUtils.isEmpty(getPhone())) {
                    mPresenter.getSmsCode(getPhone());
                }else{
                    showToast("请先输入手机号码");
                }
            }
        });
        mSubmitTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(TextUtils.isEmpty(getPhone())){
                    showToast("请输入手机号码");
                    return;
                }
                if(TextUtils.isEmpty(getSmsCode())){
                    showToast("请输入验证码");
                    return;
                }
                if(TextUtils.isEmpty(getPassword())){
                    showToast("请输入密码");
                    return;
                }
                mPresenter.getFindPassword(getPhone(),getSmsCode(),getPassword());
            }
        });
        mBackIm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected RegisterContract.Presenter createPresenter() {
        return new RegisterPresenter(this);
    }

    @Override
    public void setSubmitCanClick(int bgcolor, boolean enable) {
        mSubmitTv.setEnabled(enable);
        mSubmitTv.setBackgroundColor(getResources().getColor(bgcolor));
    }

    @Override
    public void setSmsCanClick(int textcolor, boolean enable) {
        mSmsTv.setEnabled(enable);
        mSmsTv.setTextColor(getResources().getColor(textcolor));
    }

    @Override
    public void setSmsBackTime(String msg, boolean enable) {
        mSmsTv.setEnabled(enable);
        mSmsTv.setText(msg);
    }

    @Override
    public void setSendSmsSuccess(String msg) {

    }

    @Override
    public void setRegister() {
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_forget_tip, null);
        final Dialog dialog = new Dialog(this);
        dialog.setContentView(view);
        TextView tv = view.findViewById(R.id.dialog_confirm_tv);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                finish();
            }
        });
        dialog.show();
    }

    @Override
    public void setPresenter(RegisterContract.Presenter presenter) {

    }

    private String getPhone(){
        return mAccountEt.getText().toString();
    }

    private String getSmsCode(){
        return mSmsEt.getText().toString();
    }

    private String getPassword(){
        return mPwdEt.getText().toString();
    }
}
